package com.whfc.emp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.AppEmpHealthReportImgMapper;
import com.whfc.emp.dao.AppEmpHealthReportMapper;
import com.whfc.emp.dto.AppAttachDTO;
import com.whfc.emp.dto.AppEmpHealthReportDTO;
import com.whfc.emp.entity.AppEmpHealthReport;
import com.whfc.emp.entity.AppEmpHealthReportImg;
import com.whfc.emp.enums.EmpHealthInfoType;
import com.whfc.emp.param.EmpHealthInfoAddParam;
import com.whfc.emp.param.EmpHealthInfoEditParam;
import com.whfc.emp.service.AppEmpHealthReportService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-09-02 11:00
 */
@DubboService(interfaceClass = AppEmpHealthReportService.class, version = "1.0.0")
public class AppEmpHealthReportServiceImpl implements AppEmpHealthReportService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppEmpHealthReportMapper appEmpHealthReportMapper;

    @Autowired
    private AppEmpHealthReportImgMapper appEmpHealthReportImgMapper;


    @Override
    public PageData<AppEmpHealthReportDTO> list(Integer empId, EmpHealthInfoType type, Integer pageNum, Integer pageSize) throws BizException {
        logger.info("获取防疫信息列表, empId:{}, type:{}, pageNum:{}, pageSize:{}", empId, type, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        if (empId == null || type == null) {
            return PageUtil.emptyPage();
        }
        List<AppEmpHealthReportDTO> list = appEmpHealthReportMapper.selectByEmpIdAndType(empId, type.getValue());
        PageHelper.clearPage();
        //获取图片
        if (list.isEmpty()) {
            return PageUtil.emptyPage();
        }
        for (AppEmpHealthReportDTO healthReportDTO : list) {
            List<String> imgList = appEmpHealthReportImgMapper.selectImgList(healthReportDTO.getHealthReportId());
            healthReportDTO.setImgList(imgList);
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(EmpHealthInfoAddParam param) throws BizException {
        logger.info("新增防疫信息, param:{}", param);
        AppEmpHealthReport healthReport = new AppEmpHealthReport();
        BeanUtils.copyProperties(param, healthReport);
        appEmpHealthReportMapper.insertSelective(healthReport);
        saveImg(healthReport.getId(), param.getImgList());
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(EmpHealthInfoEditParam param) throws BizException {
        logger.info("修改防疫信息列表, param:{}", param);
        AppEmpHealthReport healthReport = appEmpHealthReportMapper.selectByPrimaryKey(param.getHealthReportId());
        if (healthReport == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该防疫信息不存在");
        }
        BeanUtils.copyProperties(param, healthReport);
        appEmpHealthReportMapper.updateByPrimaryKeySelective(healthReport);
        //删除图片
        appEmpHealthReportImgMapper.logDel(healthReport.getId());
        //保存图片
        saveImg(healthReport.getId(), param.getImgList());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Integer healthInfoId) throws BizException {
        logger.info("删除防疫信息, healthInfoId:{}", healthInfoId);
        //删除防疫信息
        appEmpHealthReportMapper.logDel(healthInfoId);
        //删除防疫信息图片
        appEmpHealthReportImgMapper.logDel(healthInfoId);
    }

    @Override
    public List<AppAttachDTO> selectByEmpIdImg(Integer empId, String type) {
        return appEmpHealthReportMapper.selectByEmpIdImg(empId,type);
    }

    /**
     * 保存防疫图片
     *
     * @param healthReportId 防疫信息ID
     * @param imgList        图片列表
     */
    private void saveImg(Integer healthReportId, List<String> imgList) {
        //保存图片
        if (imgList == null || imgList.isEmpty()) {
            return;
        }
        for (String imgUrl : imgList) {
            AppEmpHealthReportImg img = new AppEmpHealthReportImg();
            img.setHealthReportId(healthReportId);
            img.setImgUrl(imgUrl);
            appEmpHealthReportImgMapper.insertSelective(img);
        }
    }

}
